var t = undefined;

var flat = {
    // 加载压平
    getFlatData: function () {
        var sTimeSecondes = new Date().getTime();
        var geojsonUrl = getRootPath() + "/attach/" + dataGuid + "/flatten.geojson?" + sTimeSecondes;
        $.ajax(
				{
				    url: geojsonUrl,
				    type: "post",
				    async: false,
				    dataType: "JSON",
				    success: function (result) {
				        console.log(result);
				        if (JSON.stringify(result) != "{}") {
				            $("#Flat").css({ "display": "block" });
				        }
				        oFlatPosArray = result;
				        flat.loadFlat("ALL");
				        for (var i in oFlatPosArray) {
				            if (i.indexOf("-name") > -1) {
				                continue;
				            }
				            flat.addFlatDoc(i, oFlatPosArray[i + "-name"]);
				        }
				    },
				    error: function (XMLHttpRequest, textStatus, errorThrown) {
				        //uploadResult = false;
				    }
				});
    },
    addFlatDoc: function (id, name) {
        // 添加压平项目
        var oFlatItem = "";
        oFlatItem += "<div class=\"layer-item-title\" flat-id=" + id + ">";
        oFlatItem += "<i class=\"\"></i>";
        oFlatItem += "<i class=\"checkicon active\"></i>";
        oFlatItem += "<span>" + name + "</span></div>";
        $("#flatBox").append(oFlatItem);
    },
    ShowFlat: function () {
        // 显示隐藏压平面
        var aCheck = $("#flatBox .checkicon");
        var aNoSelect = [];
        for (var i = 0; i < aCheck.length; i++) {
            if (!aCheck.eq(i).hasClass("active")) {
                aNoSelect.push(aCheck.eq(i).parent().attr("flat-id"));
            }
        }

        flat.loadFlat(aNoSelect);
    },
    loadFlat: function (flatId) {
        for (var i = 0; i < viewer.scene.primitives.length; i++) {
            var aTileSet = viewer.scene.primitives.get(i);
            if (aTileSet._type == "pageLOD") {
                try {
                    aTileSet.cleanflattenPolygon();
                    pressurePolygonDataSource.entities.removeAll();
                } catch (e) { }

            }
        }
        if (flatId != "CLEAN") {
            for (var i in oFlatPosArray) {
                if ($.inArray(i, flatId) < 0 || flatId == "ALL") {
                    if (i.indexOf("-name") > -1) {
                        continue;
                    }
                    var pos = oFlatPosArray[i];
                    var mtileset = undefined;
                    mtileset = whichTileset(pos[0], pos[1]);
                    if (mtileset == "") {
                        t = setTimeout("flat.loadFlat('" + flatId + "')", 2000);
                        break;
                    } else {
                        clearTimeout(t);
                    }
                    var myPolygon = new LSGlobe.PolygonGeometry({
                        polygonHierarchy: new LSGlobe.PolygonHierarchy(LSGlobe.Cartesian3.fromDegreesArrayHeights(pos)),
                        perPositionHeight: true
                    });
                    pressurePolygonDataSource.entities.add({
                        /* name: '未命名面',
                        id:i,
                        polygon: {
                        hierarchy: LSGlobe.Cartesian3.fromDegreesArrayHeights(pos),
                        material: new LSGlobe.Color(0.5, 1.0, 1.0, 0.01),
                        fill: true
                        } */
                        name: '未命名面',
                        id: i,
                        polygon: {
                            hierarchy: LSGlobe.Cartesian3.fromDegreesArrayHeights(pos),
                            material: new LSGlobe.Color(0.5, 1.0, 1.0, 0.01),
                            fill: true,  //显示填充
                            outline: false,
                            outlineColor: LSGlobe.Color.YELLOW,
                            perPositionHeight: true
                        }
                    });
                    mtileset._flattenPolygon.push(myPolygon);
                    mtileset._needUpateFlatten = true;
                }
            }
        }

    },
    init: function () {
        $(document).on("click", "#analysis-panel .layer-item.Flat .checkicon", function () {
            var isShowed = $(this).hasClass("active") ? true : false;
            if (isShowed) {//已勾选时
                $(this).removeClass("active");
                if ($(this).hasClass("box")) {
                    var oChilds = $(this).parent().next().find(".checkicon");
                    oChilds.removeClass("active");
                    flat.loadFlat("CLEAN");
                } else {
                    flat.ShowFlat();
                    var hasChild = $(this).parent().siblings().children(".checkicon").hasClass("active");
                    if (!hasChild) {//子选项全部未勾选时
                        $(this).parents(".layer-item.Flat").find(".checkicon.box").removeClass("active");
                    }
                }
            } else {//未勾选时
                $(this).addClass("active");
                if ($(this).hasClass("box")) {//父勾选子勾选
                    var oChilds = $(this).parent().next().find(".checkicon");
                    oChilds.addClass("active");
                    flat.loadFlat("ALL");
                } else {//子勾选父勾选
                    $(this).parents(".layer-item.Flat").find(".checkicon.box").addClass("active");
                    flat.ShowFlat();
                }
            }
        });
        //压平定位
        $(document).on("click", "#flatBox .layer-item-title", function () {
            var id = $(this).attr("flat-id");
            var entity = pressurePolygonDataSource.entities.getById(id);
            viewer.flyTo(entity);

        });
    }
}